<template>
	<div :class="['widgets-home', customizing ? 'customizing' : '']" ref="main">
		<div class="widgets-content">
			<el-form :model="form" :rules="rules" ref="dialogForm" :disabled="mode == 'show'" label-width="110px" label-position="right">
				<el-row :gutter="1">
					<el-col :span="24">
                        <div class="titleBox">
                            {{mode=='show'?'查看质检方案':mode == 'edit'?'编辑质检方案':'新增质检方案'}}
                            <div>
                                <el-button type="primary" @click="submit" :disabled="mode == 'show'">保存</el-button>
                                <!-- <el-button type="primary" @click="submit" :disabled="mode == 'show'">保存并新增</el-button> -->
                                <!-- <el-button type="primary" @click="submit" :disabled="mode == 'show'">审核</el-button> -->
                                <!-- <el-button type="danger" @click="submit" :disabled="mode == 'show'">退出</el-button> -->
                            </div>
                        </div>
					</el-col>
				</el-row>
				<el-card class="box-card">
					<el-row :gutter="1">
						<el-col :span="24">
							<div class="titleBox titleBox1">
								基本信息
							</div>
						</el-col>
					</el-row>
					<el-row :gutter="1">
						<el-col :xs="24" :sm="3">
							<el-form-item label="质检方案编码" prop="quality_code">
								<el-input v-model="form.quality_code" placeholder="请输入(不填写自动生成)" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="质检方案名称" prop="quality_name">
								<el-input v-model="form.quality_name" placeholder="请输入" clearable></el-input>
							</el-form-item>
						</el-col>
                        <el-col :xs="24" :sm="3">
							<el-form-item label="质检方案分类" prop="quality_cate">
                                <el-select v-model="form.quality_cate" placeholder="请选择分类" style="width:100%" clearable filterable>
                                    <el-option v-for="item in cateList" :key="item.id" :label="item.cate_name" :value="item.id"/>
                                </el-select>
							</el-form-item>
						</el-col>
                        <el-col :xs="24" :sm="3">
							<el-form-item label="检验人" prop="">
                                <el-select v-model="create_admin_id" filterable clearable placeholder="请选择检验人" style="width:100%">
                                    <el-option v-for="item in clientList" :key="item.id" :label="item.name" :value="item.id"/>
                                </el-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row :gutter="1">
						<el-col :span="24">
                            <el-button type="primary" size="small" @click="selectItem" :disabled="mode == 'show'">选择检验项目</el-button>
							<sc-form-table ref="table" v-model="form.quality_detail" :addTemplate="addTemplate" placeholder="暂无数据" height="250" hideAdd>
                                <el-table-column label="检验项目名称" prop="name" align="center"></el-table-column>
								<el-table-column label="编号" prop="code" align="center"></el-table-column>
                                <el-table-column label="检验标准" prop="standard" align="center"></el-table-column>
                                <el-table-column label="方法" prop="method" align="center"></el-table-column>
								<el-table-column prop="is_rejects" label="是否填写不良品" align="center">
                                    <template #default="scope">
                                        <el-switch v-model="scope.row.is_rejects" :active-value="1" :inactive-value="0"></el-switch>
                                    </template>
                                </el-table-column>
                                <el-table-column label="检验单位" prop="unit" align="center"></el-table-column>
                                <el-table-column label="标准值" prop="stvalue" align="center"></el-table-column>
                                <el-table-column label="检验上限" prop="upper" align="center"></el-table-column>
                                <el-table-column label="检验下限" prop="lower" align="center"></el-table-column>
                                <el-table-column label="检验说明" prop="check_content" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.check_content" placeholder="检验说明" clearable class="input"></el-input>
									</template>
                                </el-table-column>
							</sc-form-table>
						</el-col>
					</el-row>
				</el-card>
				<el-card class="box-card">
					<el-row :gutter="1">
						<el-col :span="24">
							<div class="titleBox titleBox1">
								其他信息
							</div>
						</el-col>
					</el-row>
					<el-row :gutter="1">
						<el-col :xs="24" :sm="3">
							<el-form-item label="创建人" prop="">
                                <el-select v-model="create_admin_id" filterable clearable placeholder="请选择创建人" style="width:100%" disabled>
                                    <el-option v-for="item in clientList" :key="item.id" :label="item.name" :value="item.id"/>
                                </el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="创建时间" prop="">
                                <el-date-picker v-model="create_date" type="date" placeholder="请选择日期" style="width: 100%;" :value-format="'YYYY-MM-DD'" disabled></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="修改人" prop="">
                                <el-select v-model="save_admin_id" filterable clearable placeholder="请选择修改人" style="width:100%" disabled>
                                    <el-option v-for="item in clientList" :key="item.id" :label="item.name" :value="item.id"/>
                                </el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="修改时间" prop="">
                                <el-date-picker v-model="save_date" type="date" placeholder="请选择日期" style="width: 100%;" :value-format="'YYYY-MM-DD'" disabled></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="数据状态" prop="">
                                <el-select v-model="data_status" placeholder="使用状态" style="width:100%" disabled>
                                    <el-option label="已审核" :value="1"/>
                                    <el-option label="未审核" :value="0"/>
                                </el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="使用状态" prop="">
                                <el-select v-model="use_status" placeholder="使用状态" style="width:100%" disabled>
                                    <el-option label="使用" :value="1"/>
                                    <el-option label="未使用" :value="2"/>
                                </el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</el-card>
			</el-form>
		</div>
	</div>
    <item-list v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></item-list>
</template>

<script>
import ItemList from '../inspectitem/itemList.vue'
export default {
	name: '新增BOM',
	components: {ItemList},
	data() {
		return {
			dialog: {
                save: false
			},
			//表单数据
			form: {
                id: "",
                quality_code: "",
                quality_name: "",
                quality_cate: "",
                quality_detail: [],
			},
			//验证规则
			rules: {
				goods_name: [{ required: true, message: '请输入物料名称' }],
			},
			total_data: [],
			customizing: false,
			addTemplate: {},
			mode: '',
			cateList:[],
            create_admin_id: "",
            create_date: "",
            save_admin_id: "",
            save_date: "",
            data_status: "",
            use_status: "",
			clientList:[],
		}
	},
	mounted() {
        this.getCate()
		this.getUser()
        if (this.$route.query.id) {
            this.setData()
            this.mode = this.$route.query.zhuangtai
        }
    },
	methods: {
        // 获取分类
		async getCate(){
			var res = await this.$API.production.proType.list.get();
			this.cateList = res.data
		},
        // 获取人员
        async getUser(){
            var res = await this.$API.all.getAdmin.get({is_page:0});
            this.clientList = res.data;
        },
		//表单注入数据
		async setData() {
			var reqData = {id: this.$route.query.id}
			var res = await this.$API.production.programme.detail.post(reqData);
			this.form.id = this.$route.query.id
			this.form.quality_code = res.data.quality_code
			this.form.quality_name = res.data.quality_name
			this.form.quality_cate = res.data.quality_cate
            this.create_admin_id = Number(res.data.create_staff)
            this.create_date = res.data.create_date
            this.save_admin_id = Number(res.data.update_staff)
            this.save_date = res.data.update_date
            this.data_status = res.data.status
            this.use_status = res.data.use_status
            let array = []
            res.data.quality_detail.map(item=>{
                array.push({
                    id: item.id,
                    check_project_id: item.check_project_id,
                    code: item.check_project.code,
                    name: item.check_project.name,
                    group: item.check_project.group,
                    standard: item.check_project.standard,
                    method: item.check_project.method,
                    unit: item.check_project.unit,
                    stvalue: item.check_project.stvalue,
                    upper: item.check_project.upper,
                    lower: item.check_project.lower,
                    note: item.check_project.note,
                    is_rejects: item.is_rejects,
                    check_content: item.check_content,
                })
            })
			this.form.quality_detail = array
		},
		resetForm(formName) {
			this.$refs[formName].resetFields();
            this.form.quality_detail = []
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.production.programme.edit.post(this.form);
					} else {
						res = await this.$API.production.programme.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						// this.$emit('success', this.form, this.mode)
						// this.visible = false;
						this.$message.success("操作成功")
						if (this.mode == '') {
							this.resetForm('dialogForm')
						} else {
							this.setData()
						}
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
        selectItem(){
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit')
            })
        },
        handleSuccess(data){
            let array = []
            data.map((item)=>{
                array.push({
                    check_project_id: item.id,
                    code: item.code,
                    name: item.name,
                    group: item.group,
                    standard: item.standard,
                    method: item.method,
                    unit: item.unit,
                    stvalue: item.stvalue,
                    upper: item.upper,
                    lower: item.lower,
                    note: item.note,
                })
            })
            let existingIds = this.form.quality_detail.map(item => item.check_project_id);
            for(let i = array.length - 1; i >= 0; i--){
                if(existingIds.includes(array[i].check_project_id)){
                    array.splice(i, 1);
                }
            }
            this.form.quality_detail = [...this.form.quality_detail,...array]
        },
	}
}
</script>

<style>
.sc-upload-multiple[data-v-f680f9e4] .el-upload--picture-card {
	width: 60px;
	height: 60px;
}

[data-v-f680f9e4] .el-upload-list__item {
	width: 60px;
	height: 60px;
}
</style>
<style lang="scss">
.widgets-home {
	display: flex;
	flex-direction: row;
	flex: 1;
	min-height: 100%;
	max-height: 100%;
}

.widgets-content {
	flex: 1;
	overflow: auto;
	overflow-x: hidden;
	padding: 15px 15px;
	.selectBox {
		display: flex;
		align-items: center;
	}
	.el-col {
		margin-right: 20px;
	}
	.el-form-item {
		flex-direction: column;
		justify-content: flex-start;
		align-items: flex-start;
		.el-form-item__label {
			justify-content: flex-start;
		}
		.el-form-item__content {
			width: 100%;
		}
	}
	.box-card {
		margin-bottom: 10px;
	}
	.box-card1 {
		.el-form-item__label {
			justify-content: flex-start;
			height: 0;
		}
	}
	.box-card2 {
		.el-col {
			margin-right: 0;
		}
	}
	.el-form {
		//background: #ffffff;
		//padding: 20px;
		min-height: 90%;
		.titleBox {
			font-size: 18px;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			color: #333333;
			padding-bottom: 10px;
			border-bottom: 1px solid #E6E6E6;
			margin-bottom: 14px;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		.titleBox1 {
			font-weight: 600;
			border: none;
			font-size: 16px;
		}
	}
}

.customizing .widgets-wrapper {
	margin-right: -360px
}

.customizing .widgets-wrapper .el-col {
	padding-bottom: 15px;
}

.customizing .widgets-wrapper .draggable-box {
	border: 1px dashed var(--el-color-primary);
	padding: 15px;
}

.customizing .widgets-wrapper .no-widgets {
	display: none;
}

.customizing .widgets-item {
	position: relative;
	margin-bottom: 15px;
}

@media (max-width: 992px) {
	.customizing .widgets {
		transform: scale(1) !important;
	}

	.customizing .widgets-aside {
		width: 100%;
		position: absolute;
		top: 50%;
		right: 0;
		bottom: 0;
	}

	.customizing .widgets-wrapper {
		margin-right: 0;
	}
}
</style>
